﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public struct stTimerParams
{
	public ulong commonUINT64;
}
public class Timer
{

	public delegate void OnTimeUpHandler(int timeSequence);

	public delegate void OnTimeUpWithParamsHandler(int timeSquence, stTimerParams stTimerParams);

	private OnTimeUpHandler m_timeuphandler;
	private OnTimeUpWithParamsHandler m_timeupwithparamshandler;
	private int m_loop = 1;//循环
	private int m_totaltime;
	private int m_currenttime;
	private bool m_isfinished;
	private bool m_isrunning;
	private int m_sequence;
	private stTimerParams m_timeparams;

	public int CurrentTime
	{
		get { return m_currenttime; }
	}

	public int TimeSqe
	{
		get { return m_sequence; }
	}

	public Timer(int time, int loop, OnTimeUpHandler onTimeUpHandler, int sqen)
	{
		if (loop == 0)
		{
			loop = -1;
		}
		this.m_totaltime = time;
		this.m_loop = loop;
		this.m_timeuphandler = onTimeUpHandler;
		this.m_sequence = sqen;
		this.m_currenttime = 0;
		this.m_isfinished = false;
		this.m_isrunning = true;
	}

	public Timer(int time, int loop, OnTimeUpWithParamsHandler onTimeUpWithParamsHandler, int sqen, stTimerParams stTimerParams)
	{
		if (loop == 0)
		{
			loop = -1;
		}
		this.m_totaltime = time;
		this.m_loop = loop;
		this.m_timeupwithparamshandler = onTimeUpWithParamsHandler;
		this.m_sequence = sqen;
		this.m_timeparams = stTimerParams;
		this.m_currenttime = 0;
		this.m_isrunning = true;
		this.m_isfinished = false;
	}

	public int GetLeftTime()
	{
		return (this.m_totaltime - this.m_currenttime);
	}

	public void Finish()
	{
		this.m_isfinished = true;
	}

	public bool IsFinished()
	{
		return this.m_isfinished;
	}

	public void Pause()
	{
		this.m_isrunning = false;
	}

	public void Reset()
	{
		this.m_currenttime = 0;
	}

	public void Resume()
	{
		this.m_isrunning = true;
	}

	public void ResetTotalTime(int totaltime)
	{
		if (this.m_totaltime == totaltime)
		{
			return;
		}
		this.m_totaltime = totaltime;
		this.m_currenttime = 0;
	}

	public bool IsSquenceMatched(int squence)
	{
		return this.m_sequence == squence ? true : false;
	}

	public bool IsDelegateMatched(OnTimeUpHandler onTimeUpHandler)
	{
		return m_timeuphandler == onTimeUpHandler;
	}

	public bool IsDelegateParamsMatched(OnTimeUpWithParamsHandler onTimeUpWithParamsHandler)
	{
		return m_timeupwithparamshandler == onTimeUpWithParamsHandler;
	}

	public stTimerParams GetTimerParams()
	{
		return this.m_timeparams;
	}

	public void Update(int deltetime)
	{
		if (this.m_isfinished || !this.m_isrunning)
		{
			return;
		}
		if (this.m_loop == 0)
		{
			this.m_isfinished = true;
		}
		else
		{
			this.m_currenttime += deltetime;
			if (this.m_currenttime >= this.m_totaltime)
			{
				if (this.m_timeuphandler != null)
				{
					this.m_timeuphandler(this.m_sequence);
				}
				if (this.m_timeupwithparamshandler != null)
				{
					this.m_timeupwithparamshandler(this.m_sequence, this.m_timeparams);
				}
				this.m_currenttime = 0;
				this.m_loop--;
			}
		}
		
	}
}


























